package com.intelligence.pda.ui.namepda;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.intelligence.androidlibrary.utils.SharePreferenceUtils;
import com.intelligence.pda.R;
import com.intelligence.pda.parameter.ResetNameParameter;
import com.intelligence.pda.retrofit.ApiCallback;
import com.intelligence.pda.ui.BaseActivity;
import com.intelligence.pda.ui.signout.model.LoginOutModel;
import com.intelligence.pda.utils.AppConstants;

import butterknife.Bind;

public class NamePdaActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.ll_toolbar_return)
    LinearLayout ll_toolbar_return;
    @Bind(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @Bind(R.id.et_name)
    EditText et_name;
    @Bind(R.id.tv_determine)
    TextView tv_determine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_pda);
        tv_toolbar_title.setText("修改姓名");
        listener();
    }

    private void listener() {
        ll_toolbar_return.setOnClickListener(this);
        tv_determine.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_toolbar_return://返回
                finish();
                break;
            case R.id.tv_determine://确定
                if (getView()) {
                  getname();
                }
                break;

        }
    }

    private boolean getView() {
        if (TextUtils.isEmpty(et_name.getText().toString())) {
            toastShow("姓名不能为空！");
            return false;
        }
        return true;
    }

    public void getname() {
        showProgressDialog();
        ResetNameParameter resetNameParameter = new ResetNameParameter();
        resetNameParameter.setBasicStr(SharePreferenceUtils.getString(AppConstants.BASIC_STR));
        resetNameParameter.setPersonName(et_name.getText().toString());
        addSubscription(apiStores.getResetName(resetNameParameter),
                new ApiCallback<LoginOutModel>() {
                    @Override
                    public void onSuccess(LoginOutModel model) {
                        SharePreferenceUtils.putString(AppConstants.PERSON_NAME, et_name.getText().toString());
                        toastShow("姓名修改成功");
                        finish();
                    }

                    @Override
                    public void onFailure(LoginOutModel model, String msg) {
                        toastShow(msg);
                        dismissProgressDialog();
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                        dismissProgressDialog();
                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }
}
